﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrameworkManapro.BusinessModel;
using FrameworkManapro.WebUI;
using GMAC.ReportingSystem.Business.Entities;
using System.Collections;
using System.Data;

namespace GMAC.ReportingSystem.DataAccess
{
    //-------------------------------------------------
    /// <summary>
    /// Ricardo Recaredo (31/03/2011)
    /// 
    /// DAO Class for Insurance Companie Entitie Model
    /// </summary>
    //-------------------------------------------------

    public class InsuranceCompanyDataAccess : BaseDataAccess<Business.Entities.InsuranceCompany>
    {
        public override GMAC.ReportingSystem.Business.Entities.InsuranceCompany Get(object Id)
        {
            ArrayList parameters = new ArrayList() { Id };
            
            DataTable dt = DataAccessComponent.ExecuteSelect("procInsuranceCompanyByIdSelect", parameters);

            if (dt != null && dt.Rows.Count > 0)
            {
                return ReadRow(dt.Rows[0]);
            }

            return null;
        }

        //----------------------------------------------------------------------

        public InsuranceCompanyCollection GetList()
        {
            DataTable dt = DataAccessComponent.ExecuteSelect("procInsuranceCompaniesByIdSelect", null);
            
            return ReadTable(dt) as InsuranceCompanyCollection;
        }

        //----------------------------------------------------------------------

        protected override GMAC.ReportingSystem.Business.Entities.InsuranceCompany ReadRow(System.Data.DataRow row)
        {
            InsuranceCompany company = new InsuranceCompany();
            
            if (row != null)
            {
                try
                {
                    company.Id     = int.Parse(row["Id"].ToString());
                    company.Name   = row["Name"].ToString();
                    company.Status = (GeneralEnumTypes.Status)(bool.Parse(row["Status"].ToString()) ? 1 : 0);
                }
                catch (Exception)
                {
                    throw;
                }
            }
            return company;
        }

        //----------------------------------------------------------------------

        protected override List<InsuranceCompany> ReadTable(System.Data.DataTable table)
        {
            try
            {
                InsuranceCompanyCollection insuranceCompanies = new InsuranceCompanyCollection();
                InsuranceCompany           InsuranceCompany   = new InsuranceCompany();

                if (table != null && table.Rows.Count > 0)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        InsuranceCompany = ReadRow(row);

                        if (InsuranceCompany != null)
                        {
                            insuranceCompanies.Add(InsuranceCompany);
                        }
                    }
                }
                return insuranceCompanies;
            }
            catch (Exception)
            {
                throw;
            }
        }

        //----------------------------------------------------------------------

        public InsuranceCompanyCollection Search(int? Id, string Name, PagingDetails paging)
        {
            ArrayList parameters = new ArrayList() { Id, Name, paging.PageNumber, paging.PageSize };

            try
            {
                DataSet ds = DataAccessComponent.ExecuteMultipleSelect("procInsuranceCompaniesByFilterSelect", parameters);
                
                InsuranceCompanyCollection result = null;
                
                if (ds.Tables.Count == 2)
                {
                    result = ReadTable(ds.Tables[0]) as InsuranceCompanyCollection;
                    
                    if (ds.Tables[1].Rows.Count > 0)
                    {
                        result.TotalResults = (int)ds.Tables[1].Rows[0][0];
                    }
                }
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }

        //----------------------------------------------------------------------  
    }
}
